package com.xxx.Dao;

import com.xxx.pojo.Student;
import com.xxx.util.DbUtil;

import java.sql.*;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @version v1.0
 * @Description
 * @Author admin
 * @date 2021/7/29  10:49
 * @Copyright
 */
public class StuDomeImpl implements StuDome {

    @Override
    public int insert(ArrayList<Student> list) {
        String sql = "insert into stu(name,age,height,sex,birthday,cla_id)values (?,?,?,?,?,?)";
        try (Connection connection = DbUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)
        ) {
            int i = 0;
            Iterator<Student> iterator = list.iterator();
            while (iterator.hasNext()) {
                Student student = iterator.next();
                preparedStatement.setString(1, student.getName());
                preparedStatement.setInt(2, student.getAge());
                preparedStatement.setDouble(3, student.getHeight());
                preparedStatement.setString(4, student.getSex());
                preparedStatement.setString(5, student.getBirthday());
                preparedStatement.setInt(6, student.getClaId());
                i = preparedStatement.executeUpdate();
            }
            return i;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(int StuectId) {
        return 0;
    }

    @Override
    public int update(Student student) {
        return 0;
    }

    @Override
    public ArrayList<Student> query(Student student) {
        ArrayList<Student> list = new ArrayList<>();
        try {
            Connection connection = DbUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement("select  * from stu");
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Student student1 = new Student();
                student.setId(resultSet.getInt("id"));
                student.setName(resultSet.getString("name"));
                student.setAge(resultSet.getInt("age"));
                student.setHeight(resultSet.getDouble("height"));
                Timestamp timestamp = resultSet.getTimestamp("birthday");
                Instant instant = timestamp.toInstant();
                LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
                //student1.setBirthday(localDateTime);
                student.setClaId(resultSet.getInt("cla_id"));
                list.add(student1);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public int transaction(Connection connection, ArrayList<Student> list) {
        String sql = "insert into stu( name ,age,height,sex,birthday,cla_id)values (?,?,?,?,?,?)";
        try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            Iterator<Student> iterator = list.iterator();
            int i = 0;
            while (iterator.hasNext()) {
                Student student = iterator.next();

                preparedStatement.setString(1, student.getName());
                preparedStatement.setInt(2, student.getAge());
                preparedStatement.setDouble(3, student.getHeight());
                preparedStatement.setString(4, student.getSex());
                preparedStatement.setString(5, student.getBirthday());
                preparedStatement.setInt(6, student.getClaId());
                i = preparedStatement.executeUpdate();
            }
            return i;
        } catch (SQLException throwables) {
            throw new RuntimeException("学习异常");
        }
    }
}
